As you're aware, working
with a database in MySQL involves CRUD operations. Working with databases
through Python also involves CRUD operations. The key difference is that the SQL statements
used to carry out your operations must be
processed in Python as strings. In this video, you'll
discover how to execute, create and read operations in a MySQL database
using Python. Little lemon is populating the restaurants database with the records of
upcoming bookings. Little Lemon also need to
retrieve or read this data from their database so they know which guests are
attending for dinner. You need to help Little
Lemon create and read data in the MySQL
database using Python. But first you need to
understand how to create and read data in a MySQL
database using Python. Let's get started
with creating data. So far, you've learned
how to create data in a database using an
insert statement, like an insert into command. For example, Little
Lemon must use an insert into statement
to add the names of customers and the timeslots they've booked to
the customer name and time columns in a table called bookings
in their database. However, there are a few more steps to this approach when
working with Python. Create your SQL
statement as normal, then use quotation marks to convert it to a Python
string argument. This string argument is passed to a MySQL database through a connector which parses it into a format that MySQL
can understand. The first step in the process is to write your SQL statement, then add a pair of
quotation marks to convert it to a Python
string argument. Finally, create a variable in which you can store the
query as a Python string. Next, Python sends the string to the database
through the cursor. The statement is then
executed on the database. Little lemon can use Python to add booking data from guests to their MySQL database using the MySQL insert query variable. The SQL data just needs to be
passed in a string format. As you should know by now, you can also retrieve
or query data from a database using a
select statement. A SQL select query is also the first step to complete when reading data using Python. Python can read data from a MySQL database using
a select statement. Just like with
your insert query, you create a variable in which the query can be stored
as a Python string and you write your select query. Make sure that it's
written within quotation marks to convert
it to a Python string. Little Lemon can use a read data query string object to retrieve all data from
their bookings table. They just need to write the select query as
a Python string, then use the execute module
from the cursor object. Now that you know how to create and read
data using Python, let's see if you can
help Little Lemon. For the purpose of
this demonstration, a connection has already been established between
Python, the API, under the MySQL database through the MySQL Connector Python API. Your first task
is to instantiate the cursor object from the connection using
the cursor method. The next step is to execute the MySQL insert query by passing it as an argument
to the execute method. Once the query is executed, commit the change to
the database using the commit method of
the connection object. Now, each new instance of
customer data is added to the bookings table in the database through
the MySQL insert query. For the next stage
of development, Little Lemon needs to read or retrieve the data
in their database. Some sample customer
data has been added to the database to test
the read functionality. You need to develop
the functionality and retrieve this data. As you learned earlier, the first step is to
create a SQL statement as a Python string that you
can pass to a variable. In this case, you need to create a SQL select statement that
retrieves all data from the bookings table in the database and pass
this statement as a string to the Python variable
called read data query. Now you need to pass your query to the execute module
on the cursor, just like you did
when creating data. Once the query is executed, you need to retrieve
the results using the fetch all methods
on the cursor. Create a new variable
called results, then pass the results
of your query to this variable through
the cursor object. The results variable is at list datatype and it shows each record in
the form of a tuple. The results variable is
essentially a list of tuples and each tuple is a single extracted row
from the bookings table. In this instance, the items
in each tuple in the results variable are ordered in the same way as the columns
and the bookings table. They are ordered this
way because you're reading all records
from the table. You can retrieve
the column names by creating a new variable named columns and calling the column names from
the cursor object. These values are then stored in the columns variable
for later use. Don't forget that it's also
good practice to close the cursor object and connection when you
no longer need them. You now know how to execute, create and read operations in a backend MySQL database using a front-end
Python application. That's a great start. I look forward to
guiding you through more CRUD operations in Python.